package com.hxcoder.edu.controller;

import com.hxcoder.commonutils.R;
import com.hxcoder.edu.entity.EduChapter;
import com.hxcoder.edu.entity.chapter.ChapterVo;
import com.hxcoder.edu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hxcoder
 */
@Api(tags = "章节管理")
@RestController
@RequestMapping("/edu/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @ApiOperation(value = "查询课程大纲列表", notes = "根据课程id查询课程大纲列表")
    @ApiImplicitParam(name = "courseId", value = "课程id", dataType = "String", required = true)
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {

        System.out.println("+++++++++++++++++++++++++++++++");
        System.out.println("获取课程大纲");
        System.out.println("+++++++++++++++++++++++++++++++");

        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody @ApiParam(name = "章节信息", value = "传入json格式的章节信息", required = true) EduChapter eduChapter) {

        chapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "查询章节", notes = "根据章节id查询章节")
    @ApiImplicitParam(name = "chapterId", value = "章节id", dataType = "String", required = true)
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {

        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", eduChapter);
    }

    @ApiOperation(value = "修改章节")
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody @ApiParam(name = "章节信息", value = "传入json格式的章节信息", required = true) EduChapter eduChapter) {

        chapterService.updateById(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "删除章节", notes = "根据章节id删除章节")
    @ApiImplicitParam(name = "chapterId", value = "章节id", required = true)
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {

        boolean flag = chapterService.deleteChapter(chapterId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}